<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<input id="input" type="email">
<script>
description('Tests ascii-unicode conversion of IDN email addresses on input type=email.');

var i = document.getElementById('input');
var expectValid = false;
var expectInvalid = true;

function setText(text)
{
    i.focus();
    i.value = '';
    document.execCommand('InsertText', false, text);
}

function emailCheck(expectedValue, expectedVisibleValue, expectedMismatch)
{
    shouldBeEqualToString('i.value', expectedValue);
    shouldBeEqualToString('getUserAgentShadowTextContent(i)', expectedVisibleValue);
    if (expectedMismatch == expectValid)
        shouldBeFalse('i.validity.typeMismatch');
    else
        shouldBeTrue('i.validity.typeMismatch');
}

debug('Values from UI');
setText('foo@ma\u00F1ana.com');
emailCheck('foo@xn--maana-pta.com', 'foo@ma\u00F1ana.com', expectValid);

setText('ma\u00F1ana@foo.com');
emailCheck('ma\u00F1ana@foo.com', 'ma\u00F1ana@foo.com', expectInvalid);

setText('ma\u00F1ana.com');
emailCheck('ma\u00F1ana.com', 'ma\u00F1ana.com', expectInvalid);

debug('Values from script');
i.value = 'foo@xn--t8j.com';
emailCheck('foo@xn--t8j.com', 'foo@\u304A.com', expectValid);

i.value = 'xn--t8j@foo.com';
emailCheck('xn--t8j@foo.com', 'xn--t8j@foo.com', expectValid);

i.value = 'xn--t8j.com';
emailCheck('xn--t8j.com', 'xn--t8j.com', expectInvalid);

i.value = 'foo@ma\u00F1ana.com';
emailCheck('foo@ma\u00F1ana.com', 'foo@ma\u00F1ana.com', expectInvalid);

debug('Multiple addresses');
i.multiple = true;
setText('foo@ma\u00F1ana.com, bar@\u304A.com');
emailCheck('foo@xn--maana-pta.com,bar@xn--t8j.com', 'foo@ma\u00F1ana.com, bar@\u304A.com', expectValid);

i.value = 'bar@xn--t8j.com, foo@xn--maana-pta.com';
emailCheck('bar@xn--t8j.com,foo@xn--maana-pta.com', 'bar@\u304A.com,foo@ma\u00F1ana.com', expectValid);

</script>
</body>
</html>
